package com.lpit.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lpit.constant.Constant;
import com.lpit.dto.UserDto;
import com.lpit.pojo.User;
import com.lpit.service.UserService;
import com.lpit.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class UserController {

    @Autowired
    private UserService userService;

    /*查询所有用户*/
    @PreAuthorize(value = "hasRole('管理员')")
    @GetMapping("/userList.html/{page}/{pageSize}")
    public Result<IPage<UserDto>> lookUsers(@PathVariable Integer page, @PathVariable Integer pageSize, UserDto userDto) {
        /*调用service查询*/
        IPage<UserDto> userDtoPage = userService.lookUsers(page, pageSize, userDto);
        return Result.success(userDtoPage);
    }

    /**
     * 测试接口
     * */
    @PreAuthorize(value = "hasRole('审核人员')")
    @RequestMapping("/hello.do")
    public String hello(){
        System.out.println("hello");
        return "hello";
    }

    /*根据id删除用户*/
    @DeleteMapping("/delUser.html")
    public Result<String> delUserById(User user) {
        return userService.removeById(user) ? Result.success(Constant.MESSAGE_OPERATION_SUCCEED) : Result.error(Constant.MESSAGE_OPERATION_FAIL);
    }

    /*批量操作冻结用户*/
    @PutMapping("/freezeUsers.html")
    public Result<String> lockUsers(@RequestParam("ids") List<Integer> ids, HttpServletRequest hsr) {
        /*
         * integers 用户的id集合
         * hsr.getRequestURI() ：请求路径
         * */
        return userService.updateUsersStatus(ids, hsr.getRequestURI()) ? Result.success(Constant.MESSAGE_OPERATION_SUCCEED) : Result.error(Constant.MESSAGE_OPERATION_FAIL);
    }

    /*批量操作解冻用户*/
    @PutMapping("/thawUsers.html")
    public Result<String> unlockUsers(@RequestParam("ids") List<Integer> ids, HttpServletRequest hsr) {
        /*
         * integers 用户的id集合
         * hsr.getRequestURI() ：请求路径
         * */
        return userService.updateUsersStatus(ids, hsr.getRequestURI()) ? Result.success(Constant.MESSAGE_OPERATION_SUCCEED) : Result.error(Constant.MESSAGE_OPERATION_FAIL);
    }

}
